//
//  ANScrollView.m
//  AroundTheWorld
//
//  Created by IfelseGo on 16/5/29.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "ANScrollView.h"
#import "MyCollectionViewCell.h"

#define WIDTH self.inView.frame.size.width
#define HEIGHT self.inView.frame.size.height
#define ANMaxSections 100
#define CELL @"cell"

@interface ANScrollView ()<UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
{
    
}
@property (nonatomic, weak) UIView *inView;
@property (nonatomic, strong) UICollectionView *slideCV;
@property (nonatomic, strong) UIPageControl *myPageC;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic,retain) NSArray *allArray;
@end

@implementation ANScrollView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    self.height = 220;
}

//轮播图
-(void)addSlideImage{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH, self.height);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    UICollectionView *slideCV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.height) collectionViewLayout:flowLayout];
    slideCV.backgroundColor = [UIColor whiteColor];
    slideCV.delegate = self;
    slideCV.dataSource = self;
    slideCV.pagingEnabled = YES;
    slideCV.bounces = NO;
    slideCV.showsVerticalScrollIndicator = NO;
    slideCV.showsHorizontalScrollIndicator = NO;
    [self.inView addSubview:slideCV];
    [slideCV registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:CELL];
    self.slideCV = slideCV;
    
    self.myPageC = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.height - 30, WIDTH, 20)];
    self.myPageC.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.myPageC.numberOfPages = self.allArray.count;
    self.myPageC.pageIndicatorTintColor = [UIColor grayColor];
    [self.inView addSubview:self.myPageC];
}
#pragma mark 添加定时器
-(void) addTimer{
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer ;
    
}
//移除定时器
- (void)removeTimer
{
    // 停止定时器
    [self.timer invalidate];
    self.timer = nil;
}

// 下一页
- (void)nextPage
{
    // 得到当前屏幕显示的cell的下标 屏幕只能显示一张
    NSIndexPath *currentIndexPath = [[self.slideCV indexPathsForVisibleItems] lastObject];
    NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:ANMaxSections/2];
    // 先瞬间滚动到section为50那个区的那张图上
    
    
    // 再滚动到下一张
    NSInteger nextItem = currentIndexPathReset.item +1;
    NSInteger nextSection = currentIndexPathReset.section;
    if (nextItem == self.allArray.count) {
        nextItem = 0;
        nextSection++;
    }
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
    if (self.allArray.count > 0) {
        [self.slideCV scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        [self.slideCV scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    }
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(WIDTH, 220);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (self.allArray.count > 0) {
        return ANMaxSections;
    } else {
        return 0;
    }
}

//设置多少个item(cell)
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//    NSLog(@"====%@",self.allArray);

    return self.allArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL forIndexPath:indexPath];
    SlideModel *model = [self.allArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

#pragma mark  - UICollectionViewDelegate
/**
 *  当用户即将开始拖拽的时候就调用
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}

/**
 *  当用户停止拖拽的时候就调用
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //NSLog(@"scrollViewDidEndDragging--松开");
    [self addTimer];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == 0) {
        
    } else {
        int page = (int)(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5) % self.allArray.count;
        self.myPageC.currentPage = page;
        self.myBlock(page);
    }
    
}

- (void)showInView:(UIView *)view withContents:(NSArray *)contents;
{
    
    self.allArray = [NSMutableArray arrayWithArray:contents];
    self.inView = view;
    
    [self addSlideImage];
    [self addTimer];
}

- (void)prePage;
{
    
}
@end
